package com.cennavi.minenavidemo.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.cennavi.minenavidemo.R;
import com.cennavi.minenavidemo.utils.FormatUtil;
import com.minedata.minenavi.mapdal.PoiItem;

import java.util.List;

public class PoiAdapter extends RecyclerView.Adapter<PoiAdapter.MyViewHolder> {
    private List<PoiItem> list;
    private PoiSelectListener listener;

    public PoiAdapter(List<PoiItem> list) {
        this.list = list;
    }

    public void setListener(PoiSelectListener listener) {
        this.listener = listener;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.poi_item, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        final PoiItem info = list.get(position);
        holder.name.setText(info.title);
        holder.address.setText(info.snippet);

        holder.content.setOnClickListener(v -> {
            if (listener != null) {
                listener.onPoiSelect(info);
            }
        });

        holder.dis.setText(FormatUtil.FormatDistKm(info.getDistance()));
        if (!TextUtils.isEmpty(info.typeDes)) {
            holder.kind.setText(info.typeDes);
            holder.kind.setVisibility(View.VISIBLE);
        } else {
            holder.kind.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView name, dis, address, kind;
        private View content;

        public MyViewHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.name);
            address = (TextView) itemView.findViewById(R.id.address);
            dis = (TextView) itemView.findViewById(R.id.dis);
            kind = (TextView) itemView.findViewById(R.id.kind);
            content = itemView.findViewById(R.id.poi_item_layout);
        }
    }

    public interface PoiSelectListener {

        void onPoiSelect(PoiItem info);
    }
}
